// sessionStorage 数据存储
export const setToken = function (tokenInfo) {
  sessionStorage.setItem('sa-token', JSON.stringify(tokenInfo))
}
export const getToken = function () {
  return JSON.parse(sessionStorage.getItem('sa-token'))
}
export const removeToken = function () {
  sessionStorage.removeItem('sa-token')
}
export const setUser = function (user) {
  if (typeof user === 'object') {
    sessionStorage.setItem('user', JSON.stringify(user))
  } else if (typeof user === 'string') {
    sessionStorage.setItem('user', user)
  }
}
export const getUser = function () {
  let user = sessionStorage.getItem('user')
  return user ? JSON.parse(user) : user
}
export const removeUser = function () {
  sessionStorage.removeItem('user')
}
export const setRole = function (role) {
  if (typeof role === 'object') {
    sessionStorage.setItem('role', JSON.stringify(role))
  } else if (typeof user === 'string') {
    sessionStorage.setItem('role', role)
  }
}
export const getRole = function () {
  let role = sessionStorage.getItem('role')
  return role ? JSON.parse(role) : role
}
export const removeRole = function () {
  sessionStorage.removeItem('role')
}

export const setTheme = function (theme) {
  localStorage.setItem('theme', theme)
}
export const getTheme = function () {
  return localStorage.getItem('theme')
}
/**
 @name: 设置有效期
 @description
 @author: limengqing
 @time: 2021-12-07 19:00:21
 */
export const setExpire = function (key, value, expire) {
  let obj = {
    data: value,
    time: Date.now(),
    expire: expire
  }
  localStorage.setItem(key, JSON.stringify(obj))
}

/**
 @name: 判断是否过期
 @description
 @author: limengqing
 @time: 2021-12-07 19:00:12
 */
export const getExpire = function (key) {
  let val = localStorage.getItem(key)
  if (!val) {
    return val
  }
  val = JSON.parse(val)
  if (Date.now() - val.time > val.expire) {
    localStorage.removeItem(key)
    return null
  }
  return val.data
}
